﻿using IDAdmin.Lib.DataLayer;
using IDAdmin.Lib.UI;
using IDAdmin.Lib.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace IDAdmin.Pages
{
    public partial class ItemCosumingStatistics : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void countButton_Click(object sender, EventArgs e)
        {
            DateTime dtFormDate = IDAdmin.Common.ConverterWorkData.convertStringToDateTime(countToolBar.beginDateTextBox.Text);
            DateTime dtToDate = IDAdmin.Common.ConverterWorkData.convertStringToDateTime(countToolBar.toDateTextBox.Text);
            List<DateTime> dateTimeList = IDAdmin.Common.ConverterWorkData.getDateListBetween(dtFormDate, dtToDate);
            dataPanel.Controls.Clear();
            System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch();
            watch.Start();
            dataPanel.Controls.Add(count(WebDB.getCommonConsumingData(dateTimeList)));
            watch.Stop();
            double elapsedMS = watch.ElapsedMilliseconds;
            measureLB.Text = elapsedMS.ToString() + "miliseconds";
        }

        private Table count(List<OriginGoldShopData> itemConsumingData)
        {
            List<GoldShopData> gsdList = new List<GoldShopData>();
            var itemList = itemConsumingData.GroupBy(g => new { g.ItemName, g.BaseId }).Select(s => new { s.Key.ItemName, s.Key.BaseId });
           
            var itemAndUsingNumList = (from tb in itemConsumingData group tb by new { tb.BaseId,tb.ItemName } into grp select new { grp.Key.BaseId,grp.Key.ItemName, Sum = grp.Sum(s => int.Parse(s.PurchasingNum)) }).ToList();
            int UsingNumColSum = 0;
            int UsingPeopleNumColSum = 0;

            foreach (var item in itemList)
            {
                GoldShopData gsd = new GoldShopData();
                gsd.BaseId = item.BaseId;
                gsd.ItemName = item.ItemName;
                gsd.UsingNum = itemAndUsingNumList.Where(w => w.BaseId.Equals(item.BaseId) && w.ItemName.Equals(item.ItemName)).Select(s => s.Sum).FirstOrDefault().ToString() ;
                UsingNumColSum += int.Parse(gsd.UsingNum);
                gsd.UsingPeopleNum = (from tb in itemConsumingData where tb.BaseId.Equals(item.BaseId) && tb.ItemName.Equals(item.ItemName) select new { tb.CharacterId }).Distinct().Count().ToString();
                UsingPeopleNumColSum += int.Parse(gsd.UsingPeopleNum);
                gsdList.Add(gsd);
            }


            //DataTable dt = ConverterWorkData.ToDataTable(gsdList, 3, 0);

            Table table = new Table();
            table.CssClass = "table1";
            table.CellSpacing = 1;
            TableRow rowHeader = new TableRow();
            rowHeader.Cells.AddRange
            (
                new TableCell[]
                {     
                    UIHelpers.CreateTableCell("STT",Unit.Percentage(5), HorizontalAlign.Center,"cellHeader"),
                     UIHelpers.CreateTableCell("ID vật phẩm",Unit.Percentage(5), HorizontalAlign.Center,"cellHeader"),
                    UIHelpers.CreateTableCell("Tên vật phẩm",Unit.Percentage(5), HorizontalAlign.Center,"cellHeader"),
                    UIHelpers.CreateTableCell("Số lượng tiêu hao",Unit.Percentage(5), HorizontalAlign.Center,"cellHeader"),
                    UIHelpers.CreateTableCell("Số lượng người đã tiêu hao",Unit.Percentage(5), HorizontalAlign.Center,"cellHeader")       
                }
            );

            table.Rows.Add(rowHeader);
            try
            {
                if (gsdList == null || gsdList.Count == 0)
                {
                    TableRow rowEmpty = new TableRow();
                    rowEmpty.Cells.Add(UIHelpers.CreateTableCell("<p>Không có dữ liệu!</p>", HorizontalAlign.Center, "cell1", 4));
                    table.Rows.Add(rowEmpty);
                }
                else
                {
                    //labelCountOfUser.Text = string.Format("{0:N0}", dt.Rows.Count);
                    string css;
                    int stt = 0;
                    foreach (var dr in gsdList)
                    {
                        stt += 1;
                        css = stt % 2 == 0 ? "cell1" : "cell2";
                        TableRow row = new TableRow();
                        row.Cells.AddRange
                        (
                            new TableCell[]
                           {
                                    UIHelpers.CreateTableCell(stt.ToString(),HorizontalAlign.Center,css),
                                    UIHelpers.CreateTableCell(Converter.ToString(dr.BaseId),HorizontalAlign.Center,css),
                                    UIHelpers.CreateTableCell(Converter.ToString(dr.ItemName),HorizontalAlign.Center,css),
                                    UIHelpers.CreateTableCell(Converter.ToString(dr.UsingNum),HorizontalAlign.Center,css),
                                    UIHelpers.CreateTableCell(Converter.ToString(dr.UsingPeopleNum),HorizontalAlign.Center,css)
                           }
                        );

                        table.Rows.Add(row);
                    }
                    TableRow lastRow = new TableRow();
                    lastRow.Cells.AddRange
                        (
                            new TableCell[]
                           {
                                    UIHelpers.CreateTableCell(stt.ToString(),HorizontalAlign.Center,""),
                                     UIHelpers.CreateTableCell("",HorizontalAlign.Center,""),
                                    UIHelpers.CreateTableCell("",HorizontalAlign.Center,""),
                                    UIHelpers.CreateTableCell(Converter.ToString(UsingNumColSum),HorizontalAlign.Center,""),
                                    UIHelpers.CreateTableCell(Converter.ToString(UsingPeopleNumColSum),HorizontalAlign.Center,"")
                           }
                        );
                    table.Rows.Add(lastRow);
                }
            }
            catch (Exception ex)
            {
            }
            return table;
        }

    }
}